<%--
  Author: DawnFz.com
  Date: 2022/9/14 21:01
--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="ctxPath" value="${pageContext.request.contextPath}"/>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${ctxPath}/static/layui/css/layui.css"/>
    <title>添加角色</title>
    <script src="${ctxPath}/static/layui/layui.js"></script>
    <script src="${ctxPath}/static/js/jquery-3.6.1.min.js"></script>
</head>
<body>
<fieldset class="layui-elem-field layui-field-title"
          style="margin-top: 20px;">
    <legend>添加角色页</legend>
</fieldset>

<form id="userForm" class="layui-form" action="" method="POST">


    <div class="layui-form-item" style="margin-left:20px;">
        <div class="layui-inline">
            <label class="layui-form-label">
                角色Id
            </label>
            <div class="layui-input-block" style="width:190px;">
                <input type="text" id="id"
                       name="id" autocomplete="off"
                       placeholder="请输入角色Id"
                       class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item" style="margin-left:20px;">
        <div class="layui-inline">
            <label class="layui-form-label">
                角色名称
            </label>
            <div class="layui-input-block" style="width:190px;">
                <input type="text" id="roleName"
                       name="roleName" autocomplete="off"
                       placeholder="请输入角色名称"
                       class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item" style="margin-left:20px;">
        <div class="layui-inline">
            <label class="layui-form-label">
                角色描述
            </label>
            <div class="layui-input-block" style="width:190px;">
                <input type="text" id="roleDesc"
                       name="roleDesc" autocomplete="off"
                       placeholder="请输入描述"
                       class="layui-input">
            </div>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn"
                    onclick="doSubmit();">添加角色
            </button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script>
    layui.use(
        ['form', 'layedit', 'laydate'],
        function () {
            var form = layui.form;
            var layer = layui.layer;
        }
    );


    function reloadPage() {
        window.location = "${ctxPath}/Role/viewList";
    }


    var fields = ['id', 'roleName', 'roleDesc'];
    //{1}pickData 函数
    //   抓取 fields 表示的哪些数据。
    function pickData() {
        var obj = {};  //设置一个空对象。
        for (var i = 0; i < fields.length; i++) {
            var fldName = fields[i];
            obj[fldName] = $("#" + fldName).val();
        }
        return obj;
    }

    //{2}doSubmit 函数
    function doSubmit() {
        //1. 抓取表单的数据。
        var obj = pickData();
        console.log(obj);
        $.ajax({
            url: '${ctxPath}/Role/addRole',
            type: 'post',
            data: obj,
            dataType: 'json',  //返回的数据格式 json
            success: function (resp) {
                var ret = resp.result;
                if (ret === 'success') {
                    layer.msg("新增角色成功。");
                    setTimeout(reloadPage, 2000);
                    //等下要跳转到列表页。【有代码】
                } else {
                    var cause = resp.cause;
                    layer.msg("新增角色信息失败, 原因:" + cause);
                }
            }
        });
    }
</script>
</body>
</html>


